package com.bjwl.service.impl;

import com.bjwl.dao.CardMapper;
import com.bjwl.pojo.Card;
import com.bjwl.pojo.vo.CardQueryVo;
import com.bjwl.service.AdminCardService;
import com.bjwl.utils.Log4jUtils;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @Author: dzx
 * @Date:Create：in 2020/5/11 14:24
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Service
@Transactional
public class AdminCardServiceImpl implements AdminCardService {

    @Autowired
    private CardMapper cardMapper;


    @Override
    public PageBean<Card> findAllCard(CardQueryVo vo) {
        PageBean<Card> pageBean = new PageBean<>(vo.getPageNum(),10,cardMapper.getCardTotalRecord(vo));
        pageBean.setData(cardMapper.getCardList(pageBean,vo));
        return pageBean;
    }

    @Override
    public boolean addCard(CardQueryVo vo) {
        SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");
        Calendar nowTime = Calendar.getInstance();
        String currentTime = df.format(nowTime.getTime());
        vo.setdTime(currentTime);
        vo.setState(0);
        try {
            cardMapper.addCard(vo);
        }catch (Exception e){
            Log4jUtils.LITERAL3.getLog().error(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public Card editS(Integer id) {
        return cardMapper.findCardById(id);
    }

    @Override
    public boolean updateCard(Card card) {
        try {
            cardMapper.updateCard(card);
            return true;
        }catch (Exception e) {
            Log4jUtils.LITERAL3.getLog().error(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean deleteCard(Integer id) {
        try {
            cardMapper.deleteCard(id);
            return true;
        }catch (Exception e){
            Log4jUtils.LITERAL3.getLog().error(e.getMessage());
            return false;
        }
    }
}
